//
//  ControlVC.swift
//  MazeGame
//
//  Created by orienthong on 2018/12/21.
//  Copyright © 2018 OrientHong. All rights reserved.
//

import UIKit
import SceneKit
import simd


class ControlVC: UIViewController {
    
    var previousDircetion: Direction = .forward
    var currentDirection: Direction = .forward
    let node = SCNNode()
    
    override func viewDidLoad() {
        view.backgroundColor = .white
        super.viewDidLoad()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {
            return
        }
        let previousLocationt = float2(touch.previousLocation(in: view))
        let currentLocation = float2(touch.location(in: view))
        let displacement = previousLocationt - currentLocation
        let absValue = abs(displacement)
        if absValue.x > absValue.y {
            if absValue.x >= 1 {
                if displacement.x > 0 {
                    currentDirection = .left
                    if previousDircetion == .forward || previousDircetion == .backward {
                        if absValue.y >= 1 {
                            previousDircetion = currentDirection
                        } else {
                            currentDirection = previousDircetion
                        }
                    }
                }
                else {
                    currentDirection = .right
                    if previousDircetion == .forward || previousDircetion == .backward {
                        if absValue.y >= 1 {
                            previousDircetion = currentDirection
                        } else {
                            currentDirection = previousDircetion
                        }
                    }
                }
            }
            print(currentDirection)
        }
            
        else {
            if absValue.y >= 1 {
                if displacement.y > 0 {
                    currentDirection = .forward
                    if previousDircetion == .left || previousDircetion == .right {
                        if absValue.y >= 2 {
                            previousDircetion = currentDirection
                        } else {
                            currentDirection = previousDircetion
                        }
                    }
                    //print("⬆️")
                }
                else {
                    currentDirection = .backward
                    if previousDircetion == .left || previousDircetion == .right {
                        if absValue.y >= 2 {
                            previousDircetion = currentDirection
                        } else {
                            currentDirection = previousDircetion
                        }
                    }
                    //print("⬇️")
                }
            }
            print(currentDirection)
        }
        print("\(previousLocationt) - \(currentLocation) = \(displacement)")
        
        
        SCNTransaction.animateWithDuration(0.5, completionBlock: {
            print("interesting")
        }) {
            node.rotate(by: SCNVector4Zero, aroundTarget: SCNVector3Zero)
        }
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        
    }
}
